// ASP.NET Web API Sample Application
// by Lee Dumond -- leedumond@gmail.com

namespace ContactManagerCrud.Models
{
   using System.Collections.Generic;

   internal class ContactRepository : IContactRepository
   {
      private readonly List<Contact> contacts = new List<Contact>();
      private int nextId = 1;

      public ContactRepository()
      {
         Add(new Contact
                {
                   Id = 1,
                   Name = "Allen Scharfenberg",
                   Email = "aescharfenberg@live.com",
                   Phone = "217-555-8690"
                });

         Add(new Contact
                {
                   Id = 2,
                   Name = "Mark Lovik",
                   Email = "mark.lovik@gmail.com",
                   Phone = "217-555-5877"
                });
         Add(new Contact
                {
                   Id = 3,
                   Name = "Andrew Vermie",
                   Email = "andrew.vermie@gmail.com",
                   Phone = "217-555-7382"
                });
         Add(new Contact
                {
                   Id = 4,
                   Name = "Jose Arce",
                   Email = "jose@hyperionpc.com",
                   Phone = "217-555-2200"
                });
         Add(new Contact
                {
                   Id = 5,
                   Name = "Lee Dumond",
                   Email = "leedumond@gmail.com",
                   Phone = "217-555-2627"
                });
      }

      #region IContactRepository Members

      public IEnumerable<Contact> GetAll()
      {
         return contacts;
      }

      public Contact Get(int id)
      {
         return contacts.Find(c => c.Id == id);
      }

      public Contact Add(Contact contact)
      {
         contact.Id = nextId++;
         contacts.Add(contact);

         return contact;
      }

      public void Remove(int id)
      {
         contacts.RemoveAll(c => c.Id == id);
      }

      public bool Update(Contact contact)
      {
         int index = contacts.FindIndex(c => c.Id == contact.Id);

         if (index == -1)
         {
            return false;
         }

         contacts.RemoveAt(index);
         contacts.Add(contact);

         return true;
      }

      #endregion
   }
}